import { RootState } from '@/store';
import { createSlice } from '@reduxjs/toolkit';
import { getProgramRank, getRecommendProgram } from '../service/redio';

interface IState {
  recommendedProgram: any[];
  ProgramRank: any[];
}

const initialState: IState = {
  recommendedProgram: [],
  ProgramRank: [],
};

export const getRecommendProgramStrore = () => {
  return async (dispatch: any, getState: () => RootState) => {
    const res: any = await getRecommendProgram();
    dispatch(setRecommmendedProgram(res.programs));
  };
};
export const getProgramRankStrore = () => {
  return async (dispatch: any, getState: () => RootState) => {
    const res: any = await getProgramRank();
    dispatch(setProgramRank(res.toplist));
  };
};
const RedioSlice = createSlice({
  name: 'redio',
  initialState,
  reducers: {
    setRecommmendedProgram: (state, { payload }) => {
      state.recommendedProgram = payload;
    },
    setProgramRank: (state, { payload }) => {
      state.ProgramRank = payload;
    },
  },
});

export const { setProgramRank, setRecommmendedProgram } = RedioSlice.actions;

export default RedioSlice.reducer;
